package com.alpha.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.HttpConstraint;
import javax.servlet.annotation.ServletSecurity;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.alpha.bom.Professor;
import com.alpha.dao.ExamDao;
import com.alpha.dao.StudentDao;

/**
 * Servlet implementation class ExamAddStudentsServlet
 */
@WebServlet("/ExamAddStudentsServlet")
@ServletSecurity(@HttpConstraint(rolesAllowed = {"admin"}))
public class ExamAddStudentsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private static Logger logger = Logger.getLogger(ExamAddStudentsServlet.class.getName());
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ExamAddStudentsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		logger.info(this.getClass().getSimpleName()+" get method started.");
				
		int examId = 0;
		Professor professor = null;
		String message = null;
		String success = null;
		String error = null;
		
		HttpSession session  = request.getSession();
		if(session.getAttribute("user")!=null && (session.getAttribute("user") instanceof Professor)){
			professor = (Professor) session.getAttribute("user");
		} else {
			response.sendRedirect("Login.jsp?message=User+not+found.");
			return;
		}
		
		if(request.getParameter("success")!=null){
			success = request.getParameter("success");
		} else if(request.getParameter("error")!=null){
			error = request.getParameter("error");
		} else if(request.getParameter("message")!=null){
			message = request.getParameter("message");
		}
		
		if(request.getParameter("exam_id")!=null){
			try {
				examId = Integer.parseInt(request.getParameter("exam_id"));
			} catch (Exception e) {
				logger.error(e.getMessage());
				response.sendRedirect("ExamsAdminServlet?error=Exam+not+found.");
				return;
			}
		}
		
		ExamDao eDao = new ExamDao();
		try {
			if(eDao.retrieveExamById(examId).getQuestions().size()<=0){
				response.sendRedirect("ExamsAdminServlet?error=Please+add+questions+first.");
				return;
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			return;
		}
		
		StudentDao sDao = new StudentDao();
		try {
			professor.setStudents(sDao.retrieveStudentsByProfessorId(professor.getUserId()));
		} catch (Exception e) {
			logger.error(e.getMessage());
			response.sendRedirect("ExamsAdminServlet?error=No+students+found.");
			return;
		}
		
		if(professor.getStudents()!=null){
			if(error!=null){
				response.sendRedirect("ExamAddStudents.jsp?exam_id="+examId+"&error="+error);
			} else if(success!=null){
				response.sendRedirect("ExamAddStudents.jsp?exam_id="+examId+"&success="+success);
			} else if(message!=null){
				response.sendRedirect("ExamAddStudents.jsp?exam_id="+examId+"&message="+message);
			} else {
				response.sendRedirect("ExamAddStudents.jsp?exam_id="+examId);
			}
		} else {
			response.sendRedirect("ExamsAdminServlet?error=No+students+found.");
			return;
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
